package com.virtium.vpc.action.controller;

import java.util.ArrayList;
import java.util.Iterator;

import com.virtium.vpc.dao.DAOFactory;
import com.virtium.vpc.dao.VpcSpeedDAO;
import com.virtium.vpc.hibernate.model.VpcSpeed;

public class VpcSpeedManager {
	private static ArrayList<VpcSpeed> arrVpcSpeed = null;

	public static ArrayList<VpcSpeed> getAll() {
		try {
			if (arrVpcSpeed != null && arrVpcSpeed.size() > 0)
				return arrVpcSpeed;
			VpcSpeedDAO VpcSpeedDAO = DAOFactory.getInstance()
					.getVpcSpeedDAO();
			arrVpcSpeed = new ArrayList<VpcSpeed>();
			arrVpcSpeed.addAll(VpcSpeedDAO.findAll());
		} catch (Exception ex) {
		}
		return arrVpcSpeed;
	}
	
	public static String getTextByID(int id){
		getAll();
		Iterator<VpcSpeed> iterObject = arrVpcSpeed.listIterator();
		while(iterObject.hasNext()){
			VpcSpeed object = iterObject.next();
			if(object.getId().equals(id))return object.getText();
			
		}
		return "";
	}
	
	public static void AddNew(String text){
		try{
			VpcSpeed object = new VpcSpeed();
			object.setText(text);
			VpcSpeedDAO ObjectDAO = DAOFactory.getInstance().getVpcSpeedDAO();
			ObjectDAO.save(object);
			arrVpcSpeed = null;
		}catch(Exception ex){
			
		}
	}
	
	public static void Delete(int id){
		try{
			
			VpcSpeedDAO ObjectDAO = DAOFactory.getInstance().getVpcSpeedDAO();
			VpcSpeed object = ObjectDAO.findById(id);
			ObjectDAO.delete(object);
			arrVpcSpeed = null;
		}catch(Exception ex){
			
		}
	}
}
